/**
 * 作业
 */
var localStorageManagers = require("script/localStorageManagers.js");
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	//滚动条
	mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
	//作业题目页
	mui('.tasksdiv').on('tap', '.taskdiv', openTasklist);
	//加载作业
	loadTask();
	//提交
	mui('.tasksdiv').on('tap','.submittask',submittask);
	//取消提交
	mui('.tasksdiv').on('tap','.canceltask',canceltask);
};
//打开作业题目页
function openTasklist() {
	var status=this.getAttribute('status');
	var taskid=this.getAttribute('taskid');
	var taskname=this.getAttribute('taskname');
	if (status == 3||status == 2||status == -1||status == -2) {
		mui.openWindow({
			url: 'taskmarked.html',
			id: 'selfcenter/task/taskmarked',
			extras:{taskid:taskid,taskname:taskname}
		});
	}else{
		mui.openWindow({
			url: 'taskreply.html',
			id: 'selfcenter/task/taskreply',
			extras:{taskid:taskid,taskname:taskname}
		});
		
	}
};
//loadTask
function loadTask(){
	document.getElementById('tasksdiv').innerHTML='';
	if(mobileFrame.IsNetWorkCanUse()){
		document.getElementById('nettip').style.display='none';
	    ajaxLoadTask();
	}else{
		document.getElementById('nettip').style.display='block';
	}
}
//ajaxLoadTask
function ajaxLoadTask(){
	var userid=localStorageManagers.getUseridSession();
	var url=serverAjax+'/mobile/task/getTaskListByStudentid.do';
	var w=plus.nativeUI.showWaiting();
	mui.ajax(url,{
		data:{userid:userid},
		type:'post',
		dataType:'json',
		timeout:10000,
		success:function(data){
			w.close();
			if(data==null||data.length==0)return;
			buildTasksDiv(data);
		},
		error:function(e1,e2,e3){
			w.close();
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//构建taskdiv
function buildTasksDiv(tasks){
	var tasksEle=document.getElementById('tasksdiv');
	for(var i=0;i<tasks.length;i++){
		var task=tasks[i];
		var isnew='';
		if(task.isnew==1)isnew='act';
		var status=task.status;
		var statusstr='<img src="../../images/task_5.png" class="submittask" taskid="'+task.id+'"><img src="../../images/task_2.png" style="display:none" taskid="'+task.id+'" status="'+status+'">';
		if(status==2){
			statusstr='<img src="../../images/task_3.png" class="canceltask" taskid="'+task.id+'"><img src="../../images/task_2.png" style="display:none" taskid="'+task.id+'" status="'+status+'">';
		}else if(status==3){
			statusstr='<img src="../../images/task_4.png">';
		}else if(status==-1||status==-2){
			statusstr='<img src="../../images/task_8.png">';
		}
		var begintime=task.begintime;
		var endtime=task.endtime;
		if(begintime.length<10)begintime="";
		if(endtime.length<10)endtime="不限制";
		var taskstr='<div class="taskdiv" taskid="'+task.id+'" status="'+status+'" taskname="'+task.name+'">';
			taskstr+='<div class="r1"><img src="../../images/note_1.png"><span class="c1">'+task.name+'</span><span class="new '+isnew+'">NEW!</span></div>';
		    taskstr+='<div class="r2"><span class="m1"><img src="../../images/tou1.png"></span><span class="c1">'+task.username+'</span><span class="navigate-right"></span>';
		    taskstr+='<span class="m2">'+statusstr+'</span></div>';
			taskstr+='<div class="r3"><span class="m1"><img src="../../images/task_1.png"></span><span class="c1">'+begintime+'--'+endtime+'</span></div>';
			taskstr+='</div>';
		tasksEle.innerHTML=tasksEle.innerHTML+taskstr;	
	}
};
//提交作业
function submittask(e){
	e.stopPropagation();
	var ts=this;
	var taskid=ts.getAttribute('taskid');
	var userid=localStorageManagers.getUseridSession();
	var url=serverAjax+'/mobile/task/submitOrCancelTask.do';
	//var w=plus.nativeUI.showWaiting();
	mui.ajax(url,{
		data:{userid:userid,taskid:taskid,type:1},
		type:'post',
		dataType:'json',
		timeout:10000,
		success:function(data){
			//w.close();
			if(data==null||data.length==0)return;
			if(data==1){
		    	mui.toast('提交作业成功');
	            ts.src='../../images/task_3.png';
	            ts.className='canceltask';
	            ts.parentNode.parentNode.parentNode.setAttribute('status',2);
	           
	          }else if(data==0){
	         	mui.toast('还未开始做作业');
	        }
		},
		error:function(e1,e2,e3){
			//w.close();
			ajaxErrorTip();
			console.log(e1);
		}
	});
	
}
//取消提交作业
function canceltask(e){
	e.stopPropagation();
	var ts=this;
	var taskid=ts.getAttribute('taskid');
	var userid=localStorageManagers.getUseridSession();
	var url=serverAjax+'/mobile/task/submitOrCancelTask.do';
	//var w=plus.nativeUI.showWaiting();
	mui.ajax(url,{
		data:{userid:userid,taskid:taskid,type:0},
		type:'post',
		dataType:'json',
		timeout:10000,
		success:function(data){
			//w.close();
			if(data==null||data.length==0)return;
			if(data==1){
			    mui.toast('取消提交成功');
	            ts.src='../../images/task_5.png';
	            ts.className='submittask';
	            ts.parentNode.parentNode.parentNode.setAttribute('status',1);
	           
	       }else if(data==0){
	       	    mui.toast('还未开始做作业');
	       }
		},
		error:function(e1,e2,e3){
			//w.close();
			ajaxErrorTip();
			console.log(e1);
		}
	});
	
}
